<?php

    include_once dirname(__FILE__) . '/../conf/facebook_const.php';

    // FacebookPageにリダイレクト
    function redirectFB() {
        if($_GET["app_data"] != "" && !ereg("facebook",$_SERVER['HTTP_USER_AGENT'])){
			echo '<meta http-equiv="refresh" CONTENT="0;URL=' . FB_URL . "&app_data=" . $_GET["app_data"] . '" />';
			exit;
		}
    }

    // FacebookPageのiframe内でリダイレクト
    function redirectIframeFB() {
        require_once '/var/www/html/wp-content/facebook-php-sdk/src/facebook.php';

		$facebook = new Facebook(array(
					'appId'  => APP_ID,
					'secret' => SECRET_KEY,
					'cookie' => true,
					));

		$signed_request = $facebook->getSignedRequest();
		if($signed_request["app_data"] != ""){
			echo '<meta http-equiv="refresh" CONTENT="0;URL=' . base64_decode($signed_request["app_data"]) . '" />';
			exit;
		}
    }

    // FacebookLikeボタンを取得
    function getLikeBottun() {
    	$permalink = get_permalink();

		$like_bottun  = '<iframe src="http://www.facebook.com/plugins/like.php?app_id=' . APP_ID . '&href=';
		$like_bottun .= urlencode($permalink . '?app_data=' . base64_encode( $permalink ));
		$like_bottun .= '&amp;send=false&amp;layout=button_count&amp;width=200&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=20" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:20px;" allowTransparency="true"></iframe>';		

		return $permalink . $like_bottun;
//		return $like_bottun;
    }
    
    //メニュー構成カスタムポストからグローバルメニューHTMLを出力
    function get_global_menu(){
	$q = get_posts('post_type=menucontrol');
	$menuhtml  = '<div id="top_navigation" class="menu-gnav-container">';
	$menuhtml .= '<ul id="topgnav" class="menu">';
	for($i = 1; $i <= 10; $i++){
	    $qq = get_post_custom_values('menu' . $i, $q[0]->ID);
	    if($qq[0] != ""){
		$menuhtml .= '<li id="menu-item-' . $qq[0] . '" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-' . $qq[0] . '">';
		$menuhtml .= '<a href="' . get_bloginfo("siteurl") ."/". $qq[0] . '/">' . $qq[0] . '</a>';
		$menuhtml .= '</li>';
	    }
	}
	$menuhtml .= "</ul></div>";
	
	return $menuhtml;
    }

    // OGPタグを取得
    function getOGPMeta() {
	global $post, $id;	
	setup_postdata($post);

	if (is_page_template('custom_photogallary.php')) {
	    $OGP_meta  = '<meta property="og:type" content="article" />';
    	$OGP_meta .= '<meta property="og:title" content="Photo Gallary' . get_the_title() . '" />';
	    $OGP_meta .= '<meta property="og:url" content="' . get_permalink() . '?app_data=' . base64_encode(get_permalink()) . '" />';
	    $OGP_meta .= '<meta property="og:description" content="' . get_the_title() . '" />';
   	    $OGP_meta .= '<meta property="og:site_name" content="' . get_bloginfo('name') . '" />';
   	    $OGP_meta .= '<meta property="og:image" content="' . get_the_post_thumbnail($id) . '" />';
	    $OGP_meta .= '<meta property="fb:app_id" content="' . APP_ID . '" />';
	}else if (is_single()) {
	    $OGP_meta  = '<meta property="og:type" content="article" />';
    	$OGP_meta .= '<meta property="og:title" content="「' . get_the_title() . '」" />';
	    $OGP_meta .= '<meta property="og:url" content="' . get_permalink() . '?app_data=' . base64_encode(get_permalink()) . '" />';
	    $OGP_meta .= '<meta property="og:description" content="' . get_the_title() . '" />';
   	    $OGP_meta .= '<meta property="og:site_name" content="' . get_bloginfo('name') . '" />';
        $OGP_meta .= '<meta property="og:image" content="' . get_bloginfo('template_url') . '/images/front/img_head.jpg" />';
	    $OGP_meta .= '<meta property="fb:app_id" content="' . APP_ID . '" />';
	}else if (is_attachment()) {
	    $url_large = wp_get_attachment_image_src($id, $size='large');
	    $OGP_meta  = '<meta property="og:type" content="article" />';
	    $OGP_meta .= '<meta property="og:title" content="「' . get_the_title() . '」" />';
	    $OGP_meta .= '<meta property="og:url" content="' . get_bloginfo('home') . '/?attachment_id=' . $id . '&app_data=' . base64_encode(get_bloginfo('home')) . '/?attachment_id=' . $id . '" />';   	
   	    $OGP_meta .= '<meta property="og:description" content="' . get_the_title() . '" />';
   	    $OGP_meta .= '<meta property="og:site_name" content="' . get_bloginfo('name') . '" />';
   	    $OGP_meta .= '<meta property="og:image" content="' . $url_large[0] . '" />';
	    $OGP_meta .= '<meta property="fb:app_id" content="' . APP_ID . '" />';
	}else{
	    $OGP_meta  = '<meta property="og:type" content="article" />';
    	$OGP_meta .= '<meta property="og:title" content="「' . get_the_title() . '」" />';
	    $OGP_meta .= '<meta property="og:url" content="' . get_permalink() . '?app_data=' . base64_encode(get_permalink()) . '" />';	
        $OGP_meta .= '<meta property="og:description" content="' . get_the_title() . '" />';
        $OGP_meta .= '<meta property="og:site_name" content="' . get_bloginfo('name') . '" />';
        $OGP_meta .= '<meta property="og:image" content="' . get_bloginfo('template_url') . '/images/front/img_head.jpg" />';
	    $OGP_meta .= '<meta property="fb:app_id" content="' . APP_ID . '" />';
	}
	    return $OGP_meta;
    }

?>